$(function () {
    // 封装获取用户信息的方法

    getUserInfo()

    // 为退出按钮绑定点击事件
    $('.logout').on('click', function () {
        //询问用户是否退出登录
        layer.confirm('确认退出登录吗？', { icon: 3, title: '提示' },
            function (index) {
                //1.要清空token
                localStorage.clear()
                location.href = 'login.html'

                //
                layer.clear()
            })
    })
})
function getUserInfo() {
    $.ajax({
        type: 'GET',
        url: '/my/userinfo',
        /* headers: {
            Authorization: localStorage.getItem('token')
        }, */
        success: function (res) {
            // res.data.user_pic = '//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg'
            if (!res.data) return
            const name = (res.data.nickname || res.data.username)[0].toUpperCase()
            res.data.name = name
            if (res.status === 0) {
                const htmlStr1 = template('tmpl-header', res)
                $('#hearder').html(htmlStr1)
                const htmlStr2 = template('tmpl-side', res)
                $('#left_user').html(htmlStr2)
                layui.element.render('nav', 'header-nav')
            } else {
                layer.msg('获取用户信息失败')
            }
        },
        /* complete: function (res) {
            if (res.responseJSON.message === '身份认证失败！') {
                //1.清空 localStorage
                localStorage.clear()
                //1.2  跳转到登录页面
                location.href = 'login.html'
            }
        } */
    })
}